package com.wfs.common.wechat;

import demo.DecryptUtil;
import org.weixin4j.Configuration;
import org.weixin4j.util.TokenUtil;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.weixin4j.spi.IMessageHandler;
import org.weixin4j.spi.HandlerFactory;
import org.weixin4j.util.XStreamFactory;

/**
 * @author 赵飞
 * @version V1.0
 * @Title: 微信公众平台接受消息自定义拦截器
 * @Package com.wfs.common.wechat
 * @Description:
 * @date 2017/2/15
 */
public class WeixinUrlFilter implements Filter {

	@Override
	public void init(FilterConfig config) throws ServletException {
		if (Configuration.isDebug()) {
			System.out.println("WeixinUrlFilter启动成功!");
		}
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		//微信服务器将发送GET请求到填写的URL上,这里需要判定是否为GET请求
		boolean isGet = request.getMethod().toLowerCase().equals("get");
		if (Configuration.isDebug()) {
			System.out.println("获得微信请求:" + request.getMethod() + " 方式");
			System.out.println("微信请求URL:" + request.getServletPath());
		}
		//消息来源可靠性验证

		String timestamp = request.getParameter("timestamp");// 时间戳
		String nonce = request.getParameter("nonce");       // 随机数
		String encrypt_type = request.getParameter("encrypt_type");       // 加密算法 ase
		String msg_signature = request.getParameter("msg_signature");// 微信加密签名

		//Token为weixin4j.properties中配置的Token
		String token = TokenUtil.get();
		if (isGet) {
			//1.验证消息真实性
			//http://mp.weixin.qq.com/wiki/index.php?title=验证消息真实性
			//URL为http://www.weixin4j.org/api/公众号
			//成为开发者验证
			String echostr = request.getParameter("echostr");   //
			//确认此次GET请求来自微信服务器，原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败
			if (TokenUtil.checkSignature(token, msg_signature, timestamp, nonce)) {
				response.getWriter().write(echostr);
			}
		} else {
			//确认此次GET请求来自微信服务器，原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败
//			if (!TokenUtil.checkSignature(token, msg_signature, timestamp, nonce)) {
//				//消息不可靠，直接返回
//				response.getWriter().write("");
//				return;
//			}
			//用户每次向公众号发送消息、或者产生自定义菜单点击事件时，响应URL将得到推送
			doPost(request, response);
		}
	}

	//当普通微信用户向公众账号发消息时，微信服务器将POST消息的XML数据包到开发者填写的URL上
	//用户在关注与取消关注公众号时，微信会把这个事件推送到开发者填写的URL
	//用户每次发送语音给公众号时，微信会在推送的语音消息XML数据包中，增加一个Recongnition字段
	private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {


		String timestamp = request.getParameter("timestamp");// 时间戳
		String nonce = request.getParameter("nonce");       // 随机数
		String encrypt_type = request.getParameter("encrypt_type");       // 加密算法 ase
		String msg_signature = request.getParameter("msg_signature");// 微信加密签名

		System.out.print("timestamp=["+timestamp+"];nonce=["+nonce+"];encrypt_type=["+encrypt_type+"];msg_signature=["+msg_signature+"]");

		try {
			response.setCharacterEncoding("UTF-8");
			//response.setContentType("text/xml");
			//获取POST流
			ServletInputStream in = request.getInputStream();
			if (Configuration.isDebug()) {
				System.out.println("接收到微信输入流,准备处理...");
			}
			//IMessageHandler messageHandler = HandlerFactory.getMessageHandler();
			//处理输入消息，返回结果
			//String xml = messageHandler.invoke(in);
			//返回结果
			//获取微信推送信息：
			String fromXML = XStreamFactory.inputStream2String(in);
			System.out.println("密文XML：\n"+fromXML);
			System.out.println("解密XML：\n"+ DecryptUtil.decryptMsg(msg_signature,timestamp,nonce,fromXML));
			response.getWriter().write("success");
		} catch (Exception ex) {
			ex.printStackTrace();
			response.getWriter().write("success");
		}
	}

	@Override
	public void destroy() {
	}
}
